<?php

class Exam_Model_DbTable_Examdetails extends Engine_Db_Table
{
  protected $_rowClass = 'Exam_Model_Examdetail';
  
  public function getListQuestion($exam_id){
        $questionTable  = Engine_Api::_()->getDbtable('questions','exam');
        $questionName = $questionTable->info('name');
        $examdetailName = $this->info('name');
        $select = $questionTable->select()
                       ->setIntegrityCheck(false) 
                       ->from(array('questions'=>$questionName),array('*'))
                       ->join(array('examdetails'=>$examdetailName),
                         "questions.question_id = examdetails.question_id",
                         null
                       ) 
                       ->where("examdetails.exam_id = ?",$exam_id)
                       ->order('order');
                       
        $rows = $questionTable->fetchAll($select);
        $results = array();
        if($rows){
            foreach($rows as $item){
                $results[$item->question_id] = $item;
            }
        } 
        return $results;
  }
  
   public function getLastedOrder($exam_id){
   		$tName = $this->info('name');
   		$select = $this->select()
   				->from($tName, 'max(`order`) as max')
   				->where('exam_id=?', $exam_id)
   				->limit(1);
   		$max = $this->fetchRow($select);
   		if($max->max)   			
   			return $max->max;
   		return 0;	
   }
}